﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Windows.Controls;
namespace ControlBehavior
{
    public class ControlBehaviors
    {
        public static void  Navigation(Grid layoutRoot ,  List<Control> controls)
        {
            
            foreach (UIElement uie in layoutRoot.Children)
                switch (uie.GetType().ToString())
                {
                    case "System.Windows.Controls.TextBox":
                        {
                            controls.Add((Control)uie);
                            break;
                        }

                    case "System.Windows.Controls.CheckBox":
                        {
                            controls.Add((Control)uie);
                            break;
                        }
                    case "Telerik.Windows.Controls.RadComboBox":
                        {
                            controls.Add((Control)uie);
                            break;
                        }
                    case "Telerik.Windows.Controls.RadDatePicker":
                        {
                            controls.Add((Control)uie);
                            break;
                        }
                    /*case "System.Windows.Controls.Button":
                        {
                            controls.Add((Control)uie);
                            break;
                        }*/
                }
            controls.Sort(delegate(Control c1, Control c2)
            {
                return c1.TabIndex.CompareTo(c2.TabIndex);
            });
            
        }

        public static void Navigation(Control control, List<Control> controls)
        {
            controls.Add(control);
            controls.Sort(delegate(Control c1, Control c2)
            {
                return c1.TabIndex.CompareTo(c2.TabIndex);
            });
        }
        public static void SelectNextControl(Control activeControl, bool forward, List<Control> controls)
        {
            Control cnt = null;
            Control[] listControls = controls.ToArray();
            int aCntidx = controls.LastIndexOf(activeControl);
            if (forward && aCntidx != controls.Count - 1)
                cnt = listControls[aCntidx + 1];
            else if (!forward && aCntidx != 0)
                cnt = listControls[aCntidx - 1];
            if (cnt != null)
            {
                if (cnt.Visibility.ToString().Equals("Collapsed"))
                {
                    if (controls.LastIndexOf(cnt) != (controls.Count - 1))
                    {
                        cnt = listControls[controls.LastIndexOf(cnt) + 1];
                    }
                    else
                    {
                        cnt = listControls[0];
                    }
                }

                cnt.Focus();

            }
            else
            {
                //listControls[0].Focus();
            }
        }

        public static void  Control_KeyUp(object sender, KeyEventArgs e,  List<Control> list)
        {
            if (e.Key == Key.Enter)
            {

                if (sender is TextBox)
                {
                    SelectNextControl(sender as TextBox, true,  list );
                }
                else if (sender is CheckBox)
                {
                    SelectNextControl(sender as CheckBox, true,  list);
                }
                else if (sender is RadComboBox)
                {
                    SelectNextControl(sender as RadComboBox, true,  list);
                }
                else if (sender is RadDatePicker)
                {
                    SelectNextControl(sender as RadDatePicker, true,  list);
                }
            }
            e.Handled = true;
        }
    }
}
